using UnityEngine;
using System.Collections;

public class CharacterStateStandingShot : CharacterState
{	
	public float m_fSpeed = 3.0f;
	
	// cover logic
	public float m_fDistanceToCover = 8.0f;
	public float m_fDistanceToCoverMomentum = 4.0f;
	public float m_fSecurityDistanceToCover = 0.5f;
	
	public override void enter()
	{
	}
	
	bool findCoberture()
	{
		Vector3 vDirection = c.getForward();
		vDirection.y = 0.0f;
		Ray oRay = new Ray(c.getMiddlePos(), vDirection);
		RaycastHit oHitInfo;
		float fAddedDistance = Input.GetAxis("Vertical") * m_fDistanceToCoverMomentum;
		if (Physics.Raycast(oRay, out oHitInfo, m_fDistanceToCover + fAddedDistance, 1))
		{
			Vector3 vNormal = oHitInfo.normal;
			vNormal.y = 0.0f;
			vNormal.Normalize();
			c.vTargetPosition = oHitInfo.point + (vNormal * m_fSecurityDistanceToCover);
			c.vTargetNormal = vNormal;
			c.changeState<CharacterStateToCover>();
			return true;
		}
		return false;
	}
	
	public override void update()
	{
		Vector2 vLeftStick = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
		
		// character moves depending the camera, so use the forward's projection of the camera
		Vector3 vRealForward = sCameras.Instance.getOrbitTransform().forward;
		vRealForward.y = 0.0f;
		c.setForward(vRealForward, Vector3.up);
		vRealForward.Normalize();
		
		if (!c.updateShotMode())
		{
			c.changeState<CharacterStateStanding>();
			return;
		}
		
		// find coberture
		if (Input.GetButtonDown("X"))
		{
			if (findCoberture())
			{
				return;
			}
		}
		
		// horizontal move
		Vector3 vMove = (vLeftStick.x * c.tCamera.right + vLeftStick.y * vRealForward) * m_fSpeed;

		c.oController.Move(vMove * Time.deltaTime);
	}
	
	public override void exit()
	{
	}
}
